﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HammerMUD.System;
using HammerMUD.Characters;
using HammerMUD.Rooms;
using HammerMUD.Areas;

namespace HammerMUD.Accounts
{
    public class AccountMenuUtilities
    {
        public const String AccountMenu =
            "/---------------------------------------------------------\\\n\r" +
            "|                      Account Menu                       |\n\r" +
            "\\---------------------------------------------------------/\n\r" +
            "|     1      ----    Account Information                  |\n\r" +
            "|     2      ----    Change Email                         |\n\r" +
            "|     3      ----    Change Password                      |\n\r" +
            "|     4      ----    Character chooser                    |\n\r" +
            "|     5      ----    Create character                     |\n\r" +
            "|     quit   ----    Exit the game                        |\n\r" +
            "\\---------------------------------------------------------/\n\r";

        public static void ProcessAccountMenu(String line, User user, HammerMUD.System.HammerMUD hammerMUD)
        {
            switch (user.CurrentState.AccountMenu)
            {
                case AccountMenuState.Menu:
                    ParseArgument(line, user, hammerMUD);
                    break;
                case AccountMenuState.ChangeEmail:
                    ChangeEmail(line, user);
                    break;
                case AccountMenuState.ChangePassword:
                    ChangePassword(line, user);
                    break;
                case AccountMenuState.ChangePasswordConfirmation:
                    ChangePasswordConfirmation(line, user);
                    break;
                case AccountMenuState.CharacterChooser:
                    CharacterChooser(line, user);
                    break;
                default:
                    Console.WriteLine("Error! Invalid state for AccountMenu");
                    break;
            }
        }

        public static void CharacterChooser(String line, User user)
        {
            if (line.Trim().ToLower().Equals("quit"))
            {
                user.CurrentState.AccountMenu = AccountMenuState.Menu;
                user.Send(AccountMenuUtilities.AccountMenu);
                return;
            }

            foreach (Character c in user.UserAccount.Characters)
            {
                if (c.Name.ToLower().Equals(line.ToLower().Trim()))
                {
                    user.Send(c.Name + " has been selected!");
                    user.UserAccount.CurrentCharacter = c;
                    
                    if (user.UserAccount.CurrentCharacter.CurrentRoom == null)
                    {
                        user.UserAccount.CurrentCharacter.CurrentRoom = HammerMUD.System.HammerMUD.EmptyRoom;
                        foreach (Area a in HammerMUD.System.HammerMUD.Areas)
                        {
                            Room temporaryRoom;
                            if ((temporaryRoom = a.FindRoom(HammerMUD.System.HammerMUD.SystemInfo.StartRoomNumber)) != null)
                            {
                                user.UserAccount.CurrentCharacter.CurrentRoom = temporaryRoom;
                                break;
                            }
                        }
                    }

                    user.UserAccount.CurrentCharacter.Account = user.UserAccount;
                    user.UserAccount.CurrentCharacter.CurrentRoom.Characters.Add(user.UserAccount.CurrentCharacter);
                    user.CurrentState.Base = BaseState.Command;
                    user.CurrentState.Command = CommandState.Command;
                    return;
                }
            }

            user.Send("Invalid character!\n\r Select from the following list of characters:");
            foreach (Character c in user.UserAccount.Characters)
            {
                user.Send("\t" + c.Name);
            }

            if (user.UserAccount.Characters.Count == 0)
            {
                user.Send("\tNo characters");
            }
            user.Send("To exit the character chooser, type quit");
        }

        public static void ChangePassword(String line, User user)
        {
            user.UserAccount.tempPassword = line.Trim();
            user.CurrentState.AccountMenu = AccountMenuState.ChangePasswordConfirmation;
            user.Prompt("Confirm that your password is ");
        }

        public static void ChangePasswordConfirmation(String line, User user)
        {
            if (line.Trim().Equals(user.UserAccount.tempPassword))
            {
                user.Send("Password updated.");
                user.UserAccount.Password = user.UserAccount.tempPassword;
            }
            else
            {
                user.Send("Passwords don't match!");
            }

            user.Send(AccountMenuUtilities.AccountMenu);
            user.CurrentState.AccountMenu = AccountMenuState.Menu;
        }

        public static void ChangeEmail(String line, User user)
        {
            user.UserAccount.Email = line.Trim();
            user.Send("Email updated.");
            user.Send(AccountMenuUtilities.AccountMenu);
            user.CurrentState.AccountMenu = AccountMenuState.Menu;
        }

        public static void ParseArgument(String line, User user, HammerMUD.System.HammerMUD hammerMUD)
        {
            if (line.Trim().Equals("1"))
            {
                user.Send(
                    "Account name  : " + user.UserAccount.Name + "\n\r" +
                    "Email address : " + user.UserAccount.Email + "\n\r" +
                    "Password      : " + user.UserAccount.Password + "\n\r");
            }
            else if (line.Trim().Equals("2"))
            {
                user.CurrentState.AccountMenu = AccountMenuState.ChangeEmail;
                user.Prompt("Your new email address is ");
            }
            else if (line.Trim().Equals("3"))
            {
                user.CurrentState.AccountMenu = AccountMenuState.ChangePassword;
                user.Prompt("Your new password is ");
            }
            else if (line.Trim().Equals("4"))
            {
                user.CurrentState.AccountMenu = AccountMenuState.CharacterChooser;

                user.Send("Select from the following list of characters:");
                foreach (Character c in user.UserAccount.Characters)
                {
                    user.Send("\t" + c.Name);
                }

                if (user.UserAccount.Characters.Count == 0)
                {
                    user.Send("\tNo characters");
                }
                user.Send("To exit the character chooser, type quit");
            }
            else if (line.Trim().Equals("5"))
            {
                user.CurrentState.Base = BaseState.NewCharacter;
                user.CurrentState.NewCharacter = NewCharacterState.Name;
                user.Prompt("Your character's name is ");
            }
            else if (line.Trim().ToLower().Equals("quit"))
            {
                hammerMUD.Disconnect(user);
            }
            else
            {
                user.Send("Invalid argument!");
                user.Send(AccountMenuUtilities.AccountMenu);
            }
        }
    }
}
